from pymongo import MongoClient
class Mongos(object):
    client = MongoClient()
    col = client['wyh']['student']

    @classmethod
    def insert(cls,data,flag:True):
        '''添加数据
            单条用字典 {} 多条套列表 []
        '''

        if flag:
            if isinstance(data,dict):
                res = cls.col.insert_one(data)
                return res
            else:
                return '单条数据必须是{"name":"age"},你输入的是%s'%type(data)
        else:
            if isinstance(data,list):
                for i in data:
                    if not isinstance(i,dict):
                        return '数据格式错误'
                res = cls.col.insert_many(data)
                return res
            return '多条数据必须是[{"name":"age"},{"name":"age"}],你输入的是%s'%type(data)
    #  查
    @classmethod
    def cl_find(cls,data=None,flag=True):
        if data:
            if flag:
                #查一条
                res = cls.col.find_one(data)
                return res
            else:
                # 查多条
                res = cls.col.find(data)
                data = []
                for i in res:
                    data.append(i)
                return da


if __name__ == '__main__':
    m = Mongos()
    data = '貂蝉'
    print(m.insert(data, flag=True))